﻿
@{
    ViewBag.Title = "机构管理";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<table id="tgOrg"></table>
<div id="tbar">
    <div class="tbar">
        <div class="z-toolbar">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true" onclick="ToolbarRedo();">展开</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="ToolbarUndo();">折叠</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow_refresh',plain:true" onclick="ToolbarRefresh();">刷新</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="ToolbarAdd();">增加</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="ToolbarEdit();">编辑</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cross',plain:true" onclick="ToolbarRemove();">删除</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="ToolbarUnselect();">取消选中</a>
        </div>
    </div>
</div>
@section PageSpecificScript{
    <script type="text/javascript">
        var tgOrg;//显示机构的TreeGrid
        $(document).ready(function () {
            tgOrg = $("#tgOrg").treegrid({
                url: '/Admin/UO_Organization/GetOrgInfoByCondition',
                fit: true,
                fitColumns: true,
                rownumbers: true,
                nowrap: false,
                method:'get',
                animate: true,
                idField: 'OrgId',
                treeField: 'OrgName',
                toolbar: '#tbar',
                frozenColumns: [[
                    { title: '机构编码', field: 'OrgId', width: 80, hidden: true },
                    { title: '机构名称', field: 'OrgName', width: 220 }
                ]],
                columns: [[
                    { title: '机构编码', field: 'OrgCheckDept', width: 80 },
                    { title: '平安门店ID', field: 'HospitalSubId', width: 80 },
                    { title: '主负责人', field: 'OrgManager', width: 80 },
                    { title: '联系电话', field: 'OrgTelePhone', width: 80 },
                    { title: '手机号码', field: 'OrgMobile', width: 80 },
                    { title: '传真', field: 'OrgFax', width: 80 },
                    { title: '显示顺序', field: 'OrgOrder', width: 50 }
                ]],
                onExpand: function (row) {
                    tgOrg.treegrid("unselectAll");
                },
                onCollapse: function (row) {
                    tgOrg.treegrid("unselectAll");
                },
                onContextMenu: function (e, row) {
                    e.preventDefault();
                    $(this).treegrid("unselectAll");
                    $(this).treegrid("select", row.depId);
                    $("#menu").menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                }
            });
        });
        /**展开菜单**/
        function ToolbarRedo(){
            var node = tgOrg.treegrid("getSelected");
            if (node) {
                tgOrg.treegrid("expand", node.depId);
            } else {
                tgOrg.treegrid("expandAll");
            }
        }
        /**折叠菜单**/
        function ToolbarUndo(){
            var node = tgOrg.treegrid("getSelected");
            if (node) {
                tgOrg.treegrid("collapse", node.depId);
            } else {
                tgOrg.treegrid("collapseAll");
            }
        }
        /**刷新菜单**/
        function ToolbarRefresh() {
            tgOrg.treegrid("reload");
        }
        /**增加菜单**/
        function ToolbarAdd() {
            showAppendView();
        }
        /**编辑菜单**/
        function ToolbarEdit(){
            showEditView();
        }
        /**删除菜单**/
        function ToolbarRemove() {
            removeOrg();
        }
        /**取消选中菜单**/
        function ToolbarUnselect() {
            tgOrg.treegrid("unselectAll");
        }
        /**新增、编辑、删除功能**/
        /*打开添加机构的页面机构*/
        function showAppendView() {
            $.validateUrl('/Admin/UO_Organization/EditView', append, xz.dealWith, 'Get');
        }

        /*添加机构*/
        function append() {
            var pAdd = xz.dialog({
                title: '新增机构',
                width: 670,
                height: 560,
                iconCls: 'icon-add',
                href: '/Admin/UO_Organization/EditView',
                buttons: [{
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        var f = pAdd.find('form');
                        f.form('submit', {
                            url: '/Admin/UO_Organization/AddOrg',
                            success: function (jsonData) {
                                jsonData = $.parseJSON(jsonData);
                                $.procAjaxMsg(jsonData, function () {
                                    pAdd.dialog('close');
                                    tgOrg.treegrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        pAdd.dialog('close');
                    }
                }],
                onLoad: function () {
                    var f = pAdd.find('form');
                    var orgName = f.find('input[name=OrgName]');
                    var orgParentID = f.find('input[name=OrgParentID]');

                    window.setTimeout(function () {
                        orgName.focus();
                    }, 100);

                    orgParentID.combotree({
                        url: '/Admin/UO_Organization/GetTreeOrgInfo',
                        valueField:'id',    
                        textField:'text',
                        lines: true
                    });
                }
            })
        }

        /*打开编辑机构的页面机构*/
        function showEditView() {
            $.validateUrl('/Admin/UO_Organization/EditView', edit, xz.dealWith, 'Get');
        }

        /*修改机构*/
        function edit() {
            var node = tgOrg.treegrid("getSelected");
            if (node) {
                var pAdd = xz.dialog({
                    title: '修改机构',
                    width: 670,
                    height: 560,
                    iconCls: 'icon-edit',
                    href: '/Admin/UO_Organization/EditView',
                    buttons: [{
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            var f = pAdd.find('form');
                            f.form('submit', {
                                url: '/Admin/UO_Organization/EditOrg',
                                success: function (jsonData) {
                                    jsonData = $.parseJSON(jsonData);
                                    $.procAjaxMsg(jsonData, function () {
                                        pAdd.dialog('close');
                                        tgOrg.treegrid("reload");
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    }, function () {
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    });
                                }
                            });
                        }
                    }, {
                        text: '关闭',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            pAdd.dialog('close');
                        }
                    }],
                    onLoad: function () {
                        var f = pAdd.find('form');
                        var orgName = f.find('input[name=OrgName]');
                        var orgParentID = f.find('input[name=OrgParentID]');
                        var orgPId = f.find('input[name=OrgPId]');
                        var orgCId = f.find('input[name=OrgCId]');

                        window.setTimeout(function () {
                            orgName.focus();
                        }, 100);

                        orgPId.combobox({
                            url: '/Admin/Sys_Province/GetAllProvince',
                            valueField: 'PId',
                            textField: 'PName',
                            onSelect: function (record) {
                                var url = '/Admin/Sys_City/GetAllCity?PId=' + record.PId;
                                orgCId.combobox('reload', url);
                            }
                        });

                        orgCId.combobox({
                            url: '/Admin/Sys_City/GetAllCity',
                            valueField: 'CId',
                            textField: 'CName'
                        });

                        orgParentID.combotree({
                            url: '/Admin/UO_Organization/GetTreeOrgInfo',
                            valueField: 'id',
                            textField: 'text',
                            required: true,
                            lines: true
                        });

                        /*把选中的机构信息绑定到对应的表单上*/
                        f.form('load', node);
                    }
                })
            } else {
                xz.messageAlert("提示", "请选择要修改的机构", "error");
            }
        }

        /*删除机构*/
        function removeOrg() {
            var node = tgOrg.treegrid("getSelected");
            if (node) {
                xz.messageConfirm('询问', '你确定要删除【' + node.OrgName + '】吗?', function (b) {
                    if (b) {
                        $.ajax({
                            type: 'POST',
                            url: '/Admin/UO_Organization/DelOrg',
                            data: {
                                OrgId: node.OrgId
                            },
                            cache: false,
                            dataType: 'JSON',
                            success: function (jsonData) {
                                $.procAjaxMsg(jsonData, function () {
                                    tgOrg.treegrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                });
            }
        }
    </script>
}

